package cn.bigfire.wedesk.net.codec;

import cn.bigfire.wedesk.bean.Wedesk;
import cn.bigfire.wedesk.net.fivesocket.frame.AbsFiveFrame;
import cn.bigfire.wedesk.net.fivesocket.frame.BinaryFiveFrame;
import cn.bigfire.wedesk.net.fivesocket.frame.OpCodes;
import cn.bigfire.wedesk.net.fivesocket.frame.TextFiveFrame;
import cn.bigfire.wedesk.net.pack.FramePack;
import cn.bigfire.wedesk.net.protocol.Protocol;
import cn.bigfire.wedesk.net.protocol.TypeMapper;
import cn.bigfire.wedesk.util.ByteUtil;
import cn.bigfire.wedesk.util.ProtostuffUtil;
import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

/**
 * 业务层 编解码器
 */
public class TextFiveFrameDecoder extends MessageToMessageDecoder<TextFiveFrame> {

    @Override
    protected void decode(ChannelHandlerContext ctx, TextFiveFrame msg, List<Object> out) {
        ByteBuf content = msg.content();
        byte[] data = new byte[content.readableBytes()];
        content.readBytes(data);

        FramePack framePack = new FramePack();
        framePack.setOp(OpCodes.TEXT_CODE);
        framePack.setLen(content.readableBytes());
        framePack.setData(data);
        ctx.fireChannelRead(framePack);
    }

}
